package com.ruoyi.controller;


import com.ruoyi.domain.ModuleInfo;
import com.ruoyi.platform.annotation.Log;
import com.ruoyi.platform.core.controller.BaseController;
import com.ruoyi.platform.core.domain.AjaxResult;
import com.ruoyi.platform.core.page.TableDataInfo;
import com.ruoyi.platform.enums.BusinessType;
import com.ruoyi.platform.utils.poi.ExcelUtil;
import com.ruoyi.service.IModuleInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 组件Controller
 *
 * @author tp
 * @date 2022-11-02
 */
@RestController
@RequestMapping("/admin/module")
@Api(tags = {"组件服务"})
public class ModuleInfoController extends BaseController {

    @Autowired
    private IModuleInfoService moduleInfoService;

    /**
     * 查询组件列表
     */
    @PreAuthorize("@ss.hasPermi('admin:module:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询组件列表", httpMethod = "GET", notes = "查询组件列表")
    public TableDataInfo list(ModuleInfo moduleInfo) {
        startPage();
        List<ModuleInfo> list = moduleInfoService.selectModuleInfoList(moduleInfo);
        return getDataTable(list);
    }

    /**
     * 导出组件列表
     */
    @PreAuthorize("@ss.hasPermi('admin:module:export')")
    @Log(title = "组件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出组件列表", httpMethod = "POST", notes = "导出组件列表")
    public void export(HttpServletResponse response, ModuleInfo moduleInfo) {
        List<ModuleInfo> list = moduleInfoService.selectModuleInfoList(moduleInfo);
        ExcelUtil<ModuleInfo> util = new ExcelUtil<>(ModuleInfo.class);
        util.exportExcel(response, list, "组件数据");
    }

    /**
     * 获取组件详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:module:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取组件详细信息", httpMethod = "GET", notes = "获取组件详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(moduleInfoService.selectModuleInfoById(id));
    }

    /**
     * 新增组件
     */
    @PreAuthorize("@ss.hasPermi('admin:module:add')")
    @Log(title = "组件", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增组件", httpMethod = "POST", notes = "新增组件")
    public AjaxResult add(@RequestBody ModuleInfo moduleInfo) {
        return toAjax(moduleInfoService.insertModuleInfo(moduleInfo));
    }

    /**
     * 修改组件
     */
    @PreAuthorize("@ss.hasPermi('admin:module:edit')")
    @Log(title = "组件", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改组件", httpMethod = "PUT", notes = "修改组件")
    public AjaxResult edit(@RequestBody ModuleInfo moduleInfo) {
        return toAjax(moduleInfoService.updateModuleInfo(moduleInfo));
    }

    /**
     * 删除组件
     */
    @PreAuthorize("@ss.hasPermi('admin:module:remove')")
    @Log(title = "组件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除组件", httpMethod = "DELETE", notes = "删除组件")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(moduleInfoService.deleteModuleInfoByIds(ids));
    }
}
